package com.waves.task.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waves.task.domain.dto.TaskCommentPageDTO;
import com.waves.task.domain.entity.TaskComment;
import com.waves.task.mapper.TaskCommentMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class TaskCommentDao extends ServiceImpl<TaskCommentMapper, TaskComment> {

    public Page<TaskComment> pageTaskComment(TaskCommentPageDTO dto) {
        LambdaQueryWrapper<TaskComment> wrapper = new LambdaQueryWrapper<TaskComment>()
                .eq(dto.getTaskId() != null, TaskComment::getTaskId, dto.getTaskId())
                .eq(dto.getType() != null, TaskComment::getType, dto.getType())
                .eq(dto.getCreatedBy() != null, TaskComment::getCreatedBy, dto.getCreatedBy());
        return baseMapper.selectPage(new Page<>(dto.getPageNum(), dto.getPageSize()), wrapper);
    }

    public List<TaskComment> selectHighComments(List<Long> taskIds) {
        LambdaQueryWrapper<TaskComment> wrapper = new LambdaQueryWrapper<TaskComment>()
                .groupBy(TaskComment::getTaskId)
                .in(TaskComment::getTaskId, taskIds)
                .orderByDesc(TaskComment::getLikedNum)
                .orderByDesc(TaskComment::getCreatedTime);
        return baseMapper.selectList(wrapper);
    }

    public List<Long> getCommentTaskIds(Long userId) {
        return baseMapper.selectList(new LambdaQueryWrapper<TaskComment>()
                .eq(TaskComment::getCreatedBy, userId)).stream()
                .map(TaskComment::getTaskId).distinct().collect(Collectors.toList());
    }
}
